load("//bazel:tensorstore.bzl", "tensorstore_cc_library", "tensorstore_cc_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

tensorstore_cc_library(
    name = "gcs_http",
    srcs = [
        "gcs_key_value_store.cc",
        "object_metadata.cc",
    ],
    hdrs = ["object_metadata.h"],
    deps = [
        ":gcs_resource",
        ":shared_auth_provider",
        "//tensorstore:context",
        "//tensorstore:json_serialization_options_base",
        "//tensorstore/internal:concurrency_resource",
        "//tensorstore/internal:data_copy_concurrency_resource",
        "//tensorstore/internal:env",
        "//tensorstore/internal:intrusive_ptr",
        "//tensorstore/internal:path",
        "//tensorstore/internal:retries_context_resource",
        "//tensorstore/internal:source_location",
        "//tensorstore/internal:uri_utils",
        "//tensorstore/internal/cache_key",
        "//tensorstore/internal/http",
        "//tensorstore/internal/http:default_transport",
        "//tensorstore/internal/http:http_header",
        "//tensorstore/internal/json",
        "//tensorstore/internal/json_binding",
        "//tensorstore/internal/json_binding:absl_time",
        "//tensorstore/internal/json_binding:bindable",
        "//tensorstore/internal/log:verbose_flag",
        "//tensorstore/internal/metrics",
        "//tensorstore/internal/oauth2",
        "//tensorstore/internal/rate_limiter",
        "//tensorstore/internal/thread:schedule_at",
        "//tensorstore/kvstore",
        "//tensorstore/kvstore:batch_util",
        "//tensorstore/kvstore:byte_range",
        "//tensorstore/kvstore:common_metrics",
        "//tensorstore/kvstore:generation",
        "//tensorstore/kvstore:key_range",
        "//tensorstore/kvstore/gcs:gcs_resource",
        "//tensorstore/kvstore/gcs:validate",
        "//tensorstore/kvstore/http:byte_range_util",
        "//tensorstore/serialization",
        "//tensorstore/util:executor",
        "//tensorstore/util:future",
        "//tensorstore/util:quote_string",
        "//tensorstore/util:result",
        "//tensorstore/util:status",
        "//tensorstore/util:str_cat",
        "//tensorstore/util/execution",
        "//tensorstore/util/execution:any_receiver",
        "//tensorstore/util/garbage_collection",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/container:btree",
        "@abseil-cpp//absl/flags:flag",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/random",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:cord",
        "@abseil-cpp//absl/strings:str_format",
        "@abseil-cpp//absl/synchronization",
        "@abseil-cpp//absl/time",
        "@nlohmann_json//:json",
    ],
    alwayslink = 1,
)

tensorstore_cc_test(
    name = "gcs_key_value_store_test",
    size = "small",
    srcs = [
        "gcs_key_value_store_test.cc",
        "gcs_mock.cc",
        "gcs_mock.h",
    ],
    flaky = 1,  # This test has large timing variations, which can cause failures.
    deps = [
        ":gcs_http",
        "//tensorstore:context",
        "//tensorstore:json_serialization_options_base",
        "//tensorstore/internal:global_initializer",
        "//tensorstore/internal:uri_utils",
        "//tensorstore/internal/http",
        "//tensorstore/internal/http:default_transport",
        "//tensorstore/internal/http:http_header",
        "//tensorstore/internal/http:mock_http_transport",
        "//tensorstore/internal/oauth2:google_auth_provider",
        "//tensorstore/internal/oauth2:google_auth_test_utils",
        "//tensorstore/internal/testing:json_gtest",
        "//tensorstore/internal/thread:schedule_at",
        "//tensorstore/kvstore",
        "//tensorstore/kvstore:batch_util",
        "//tensorstore/kvstore:byte_range",
        "//tensorstore/kvstore:generation",
        "//tensorstore/kvstore:key_range",
        "//tensorstore/kvstore:test_matchers",
        "//tensorstore/kvstore:test_util",
        "//tensorstore/util:future",
        "//tensorstore/util:result",
        "//tensorstore/util:status_testutil",
        "//tensorstore/util:str_cat",
        "//tensorstore/util/execution",
        "//tensorstore/util/execution:sender_testutil",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/hash",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/random:distributions",
        "@abseil-cpp//absl/status",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:cord",
        "@abseil-cpp//absl/synchronization",
        "@abseil-cpp//absl/time",
        "@googletest//:gtest_main",
        "@nlohmann_json//:json",
        "@re2",
    ],
)

tensorstore_cc_test(
    name = "object_metadata_test",
    size = "small",
    srcs = [
        "object_metadata_test.cc",
    ],
    deps = [
        ":gcs_http",
        "//tensorstore/kvstore:test_util",
        "//tensorstore/util:result",
        "@abseil-cpp//absl/time",
        "@googletest//:gtest_main",
    ],
)

tensorstore_cc_library(
    name = "gcs_resource",
    srcs = ["gcs_resource.cc"],
    hdrs = ["gcs_resource.h"],
    deps = [
        "//tensorstore:context",
        "//tensorstore/internal:env",
        "//tensorstore/internal/cache_key",
        "//tensorstore/internal/json_binding",
        "//tensorstore/internal/json_binding:absl_time",
        "//tensorstore/internal/json_binding:bindable",
        "//tensorstore/internal/log:verbose_flag",
        "//tensorstore/internal/rate_limiter",
        "//tensorstore/internal/rate_limiter:admission_queue",
        "//tensorstore/internal/rate_limiter:scaling_rate_limiter",
        "//tensorstore/util:result",
        "@abseil-cpp//absl/base",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/flags:marshalling",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/time",
    ],
    alwayslink = 1,
)

tensorstore_cc_library(
    name = "shared_auth_provider",
    srcs = ["shared_auth_provider.cc"],
    hdrs = ["shared_auth_provider.h"],
    deps = [
        "//tensorstore/internal/http",
        "//tensorstore/internal/oauth2",
        "//tensorstore/internal/oauth2:google_auth_provider",
        "//tensorstore/util:result",
        "@abseil-cpp//absl/base:core_headers",
        "@abseil-cpp//absl/synchronization",
    ],
)

tensorstore_cc_test(
    name = "gcs_testbench_test",
    size = "medium",
    srcs = [
        "gcs_testbench_test.cc",
    ],
    args = ["--testbench_binary=$(location @com_google_storagetestbench//:rest_server)"],
    data = ["@com_google_storagetestbench//:rest_server"],
    flaky = 1,
    tags = [
        "cpu:2",
        "requires-net:loopback",
        "skip-cmake",  # storage_testbench is python, which doesn't work in bazel_to_cmake yet.
        "skip-darwin",  # the python binary doesn't find dependencies on darwin.
    ],
    deps = [
        ":gcs_http",
        "//tensorstore/internal:env",
        "//tensorstore/internal:global_initializer",
        "//tensorstore/internal/http:transport_test_utils",
        "//tensorstore/internal/thread",
        "//tensorstore/kvstore",
        "//tensorstore/kvstore:generation",
        "//tensorstore/kvstore:test_util",
        "//tensorstore/kvstore/gcs:gcs_testbench",
        "//tensorstore/util:future",
        "//tensorstore/util:result",
        "//tensorstore/util:status_testutil",
        "@abseil-cpp//absl/base",
        "@abseil-cpp//absl/base:no_destructor",
        "@abseil-cpp//absl/log:absl_check",
        "@abseil-cpp//absl/log:absl_log",
        "@abseil-cpp//absl/strings",
        "@abseil-cpp//absl/strings:cord",
        "@googletest//:gtest_main",
    ],
)
